import { Component, OnInit } from '@angular/core';
import { NzFormatEmitEvent } from 'ng-zorro-antd/tree';
import { MockService } from 'src/app/services/mock.service';
import {
  switchMap,
  takeUntil,
  map,
  catchError,
  combineAll,
  concatAll,
} from 'rxjs/operators';
import { combineLatest, forkJoin, interval, of, Subject, timer } from 'rxjs';
import { NzMessageService } from 'ng-zorro-antd/message';

@Component({
  selector: 'app-other',
  templateUrl: './other.component.html',
  styleUrls: ['./other.component.scss'],
})
export class OtherComponent implements OnInit {
  listOfOption = ['Option 01', 'Option 02'];
  listOfSelectedValue = ['Default 01', 'Default 02'];
  defaultOption = [...this.listOfSelectedValue];

  selectedValue = 'Default';

  defaultCheckedKeys = ['0-0-0'];

  nodes = [
    {
      title: '0-0',
      key: '0-0',
      expanded: true,
      children: [
        {
          title: '0-0-0',
          key: '0-0-0',
          children: [
            { title: '0-0-0-0', key: '0-0-0-0', isLeaf: true },
            { title: '0-0-0-1', key: '0-0-0-1', isLeaf: true },
            { title: '0-0-0-2', key: '0-0-0-2', isLeaf: true },
          ],
        },
        {
          title: '0-0-1',
          key: '0-0-1',
          children: [
            { title: '0-0-1-0', key: '0-0-1-0', isLeaf: true },
            { title: '0-0-1-1', key: '0-0-1-1', isLeaf: true },
            { title: '0-0-1-2', key: '0-0-1-2', isLeaf: true },
          ],
        },
        {
          title: '0-0-2',
          key: '0-0-2',
          isLeaf: true,
        },
      ],
    },
    {
      title: '0-1',
      key: '0-1',
      children: [
        { title: '0-1-0-0', key: '0-1-0-0', isLeaf: true },
        { title: '0-1-0-1', key: '0-1-0-1', isLeaf: true },
        { title: '0-1-0-2', key: '0-1-0-2', isLeaf: true },
      ],
    },
    {
      title: '0-2',
      key: '0-2',
      isLeaf: true,
    },
  ];

  query1List:any = [];

  destory$ = new Subject();

  constructor(private mock: MockService, private message: NzMessageService) {}

  ngOnInit(): void {
    this.initQueryData();
  }

  ngOnDestroy(): void {
    this.destory$.next();
    this.destory$.complete();
  }

  testFun() {}

  nzEvent(event: NzFormatEmitEvent): void {

  }

  initQueryData() {
    const query1$ = this.queryData(0);
    const query2$ = this.queryData(0);
    const query3$ = this.queryData(0);
    const initQueryList = [query1$, query2$, query3$];

    of(initQueryList)
      .pipe(
        takeUntil(this.destory$),
        switchMap((value) => {
          return value;
        }),
        combineAll()
      )
      .subscribe(([query1, query2,query3]) => {

        this.query1List = query1.value.map((item: any) => {
          item['linkUrl'] = this.getUrlByCode(item.code)
          return item;
        })

      });
  }

  getUrlByCode(code: string) {
    const severUrl = 'https://ng.ant.design';
    const module = 'components';
    const language = 'zh'
    return `${severUrl}/${module}/${code}/${language}`
  }

  request(type: number) {
    const params = {
      code: 'code111',
      name: `query${type + 1}`,
    };
    const typeMapping = {
      queryData1: this.mock.queryData1(params),
      queryData2: this.mock.queryData2(params),
      queryData3: this.mock.queryData3(params),
    };

    const map = [
      typeMapping.queryData1,
      typeMapping.queryData2,
      typeMapping.queryData3,
    ];
    const request$ = map[type];
    return request$;
  }

  queryData(type: number) {
    return this.request(type).pipe(
      takeUntil(this.destory$),
      map((res: any) => {
        this.responseError(res);
        return res;
      }),
      catchError((err: any) => {

        return of(null);
      })
    );
  }

  responseError(err: any) {
    if (err.code !== '00000') {
      this.message.create('error', `This is a message of error`);
      throw new Error(err.message);
    }
    if (!err.value) {
      throw new Error('no value');
    }
  }
}
